package com.nero.service.impl;

import com.nero.bean.PO.TbAdmin;
import com.nero.bean.PO.TbAdminExample;
import com.nero.bean.PO.TbUser;
import com.nero.dao.TbAdminMapper;
import com.nero.dao.TbUserMapper;
import com.nero.service.IUserService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Nero on 18/8/6.
 */
@Service
public class UserServiceImpl implements IUserService {
//    @Resource
//    TbUserMapper tbUserMapper;
    @Autowired
    TbAdminMapper tbAdminMapper;

    public TbAdminMapper getTbAdminMapper() {
        return tbAdminMapper;
    }

    public void setTbAdminMapper(TbAdminMapper tbAdminMapper) {
        this.tbAdminMapper = tbAdminMapper;
    }

    Logger logger = LogManager.getLogger(this.getClass());

    public TbUser Login(TbUser tbUser) {
//        TbUser form = tbUser;
//        TbUserExample example = new TbUserExample();
//        TbUserExample.Criteria c = example.createCriteria();
//        c.andUsernameEqualTo(form.getUsername());
//        c.andPasswordEqualTo(form.getPassword());
//        List<TbUser> found = tbUserMapper.selectByExample(example);
//        if (found.size() == 1) {
//            System.out.println("登录成功");
//            return found.get(0);
//        }
//        System.out.println("登录失败");
        return null;
    }

    @Override
    public TbAdmin findByUsername(String username) {
        TbAdminExample example = new TbAdminExample();
        TbAdminExample.Criteria c = example.createCriteria();
        c.andUsernameEqualTo(username);
        logger.info("username: " + username);
        logger.info("tbAdminMapper's class: " + tbAdminMapper.getClass().toString());
        List<TbAdmin> found = tbAdminMapper.selectByExample(example);
        logger.info("found: ");
        for (TbAdmin tbAdmin : found) {
            logger.info(tbAdmin.toString());
        }
        return found.get(0);
    }
}
